package com.atguigu.crowd.mvc.handler;

import com.atguigu.crowd.entity.Admin;
import com.atguigu.crowd.entity.ParamData;
import com.atguigu.crowd.service.api.AdminService;
import com.atguigu.crowd.util.ResultEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@Controller
public class TestHandler {

    @Autowired
    private AdminService adminService;

    @RequestMapping("/test/ssm")
    public String testHandler(ModelMap modelMap) {
        List<Admin> adminList = adminService.getAll();
        modelMap.addAttribute("adminList", adminList);
        return "target";
    }

    @ResponseBody
    @RequestMapping("/test/requestBody1")
    public ResultEntity testRequestBody1(@RequestParam("arr[]") List<Integer> arr) {
        System.out.println(arr.toString());
        return ResultEntity.successWithoutData();
    }

    @ResponseBody
    @RequestMapping("/test/requestBody2")
    public ResultEntity testRequestBody2(ParamData paramData) {
        System.out.println(paramData.getArray().toString());
        return ResultEntity.failed("测试一下failed");
    }

    @ResponseBody
    @RequestMapping("/test/requestBody3")
    public ResultEntity<List<Integer>> testRequestBody3(@RequestBody Integer[] arr) {
        List<Integer> list = new ArrayList<>();
        Collections.addAll(list, arr);

        return ResultEntity.successWithData(list);
    }
}
